package com.midou.administration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.midou.administration.bo.CouponBo;
import com.midou.administration.bo.CouponBob;
import com.midou.administration.entity.Coupon;
import com.midou.administration.entity.UserCoupon;
import com.midou.administration.mapper.CouponMapper;
import com.midou.administration.mapper.UserCouponMapper;
import com.midou.administration.service.CouponService;
import org.checkerframework.checker.units.qual.A;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MrChen
 * @since 2021-03-24
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private  UserCouponMapper userCouponMapper;

    public Page Select(CouponBob couponBo){
        List<CouponBo> list=couponMapper.All(couponBo.getPage(),couponBo.getBusinessId());
        for (CouponBo bo : list) {
            int num=bo.getCpEndTime().compareTo(new Date());
            System.out.println(num);
            if (num<1){
                QueryWrapper queryWrapper=new QueryWrapper();
                queryWrapper.eq("uc_id",(bo.getCpId()));
                UserCoupon userCoupon=userCouponMapper.selectOne(queryWrapper);
                userCoupon.setUcState(1);
                userCouponMapper.updateById(userCoupon);
            }
        }
        List<CouponBo> list1=couponMapper.All(couponBo.getPage(),couponBo.getBusinessId());
        couponBo.getPage().setRecords(list);
        return couponBo.getPage();
    }

    public int queryAdd(CouponBo couponBo){
        return couponMapper.queryAdd(couponBo);
    }

    /**
     * 查询该用户下所有优惠卷信息
     */
    public List<CouponBo> findDoUseridAll(Integer id){
        List<CouponBo> list = couponMapper.findDoUseridAll(id);
        return list;
    }

    /**
     * 查询未结束
     * @return
     */
    @Override
    public List<Coupon> queryAll() {
        return this.couponMapper.queryAllByCpEndTimeList();
    }

    //根据id查询具体活动
    @Override
    public Coupon selectByid(Integer id){
        return this.couponMapper.selectById(id);
    }

    @Override
    public List<Coupon> queryByState(Integer id) {
        return this.couponMapper.queryByState(id);
    }

    @Override
    public int updListState(List<Coupon> list) {
        return this.couponMapper.updList(list,1);
    }


    /**
     * 按照用户id查询名下所有优惠卷
     */
    public List<CouponBo> findDoUserAll(int id){
        List<CouponBo> list = couponMapper.findDoUseridAll(id);
        return list;
    }

    public int queryDlet(Integer id){
        int c=couponMapper.deleteById(id);
        return c;
    }

    @Override
    public int updateKucun(Integer integer) {
        int count = this.couponMapper.updateNum(integer);
        return count;
    }

    //开始 但是未结束的活动信息
    @Override
    public List<Coupon> kaishi() {
        return this.couponMapper.kaishi();
    }

    //未开始的活动信息
    @Override
    public List<Coupon> weikaishi() {
        return this.couponMapper.weikaishi();
    }
}
